#include "lyhandzhe.h"
#include <QTimer>
#include <QMouseEvent>
#include <QUrl>

LYHandZHE::LYHandZHE(QWidget *parent)
    : QWidget(parent), roleLabel(new QLabel(this)), currentFrame(0)
{
    setWindowFlags(Qt::WindowType::FramelessWindowHint);    //无边框
    setAttribute(Qt::WA_TranslucentBackground);             //背景透明

    this->installEventFilter(this);                         //装载函数

    //设置音乐播放
    player = new QMediaPlayer(this);
    player->setMedia(QUrl("D:/QtResources/SpringSonata.mp3"));
    player->setVolume(100);

    //设置图片切换间隔
    QTimer *updateTimer = new QTimer(this);
    updateTimer->callOnTimeout(this, &LYHandZHE::updateRoleAnimation);
    updateTimer->start(1200);

    updateRoleAnimation();          //调用函数

    roleLabel->resize(700,700);     //设置窗口大小
}

LYHandZHE::~LYHandZHE()
{

}

void LYHandZHE::updateRoleAnimation()
{
    QString qss("background-repeat:no-repeat;");        //只展示一张图片

    //循环播放图片
    roleLabel->setStyleSheet(qss+QString("background-image:url(://C:/Users/wHy/Pictures/QtResources/LYHandZHE/%1.jpg)").arg(currentFrame));
    currentFrame = (currentFrame + 1) % 9 ;
}

bool LYHandZHE::eventFilter(QObject *watched, QEvent *ev)
{
    QMouseEvent * mouseev = static_cast<QMouseEvent*>(ev);

    static QPoint begpos;       //鼠标点击的坐标

    //当鼠标点击时，记录点击点的坐标
    if(ev->type() == QEvent::MouseButtonPress)
    {
        begpos = mouseev->globalPos() - this->pos();

    }

    //拖动窗口
    else if(ev->type() == QEvent::MouseMove && mouseev->buttons()&Qt::MouseButton::LeftButton)
    {
        this->move(mouseev->globalPos() - begpos);
    }

    //右键图片播放音频
    if(ev->type() == QEvent::MouseButtonPress && mouseev->buttons()&Qt::MouseButton::RightButton)
    {
        player->play();
    }

    return false;
}
